﻿<%@ Page Language="VB" MasterPageFile="~/Default.master" Title="Clubsite Links" %>
<%@ Register TagPrefix="Club" TagName="LoginBanner" Src="~/UserControls/LoginBanner.ascx" %>

<script runat="server">
    Dim DeleteID As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        If CStr(Request("deleteid")) Is Nothing Then
            If CStr(Request("editid")) Is Nothing Then
                FormView1.ChangeMode(FormViewMode.Insert)
            Else
                FormView1.ChangeMode(FormViewMode.Edit)
            End If
        Else
            SqlDataSource1.DeleteParameters.Add("linkid", CStr(Request("deleteid")))
            SqlDataSource1.Delete()
            Response.Redirect("~/links/links.aspx")
        End If
        If User.IsInRole("Administrators") Then
            EditPanel.Visible = True
        Else
            EditPanel.Visible = False
        End If
End Sub

    Protected Sub FormView1_ItemUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewUpdatedEventArgs)
        Response.Redirect("~/links/links.aspx")
    End Sub


    Protected Sub FormView1_ItemInserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertedEventArgs)
        Response.Redirect("~/links/links.aspx")
    End Sub

    Protected Sub FormView1_ItemDeleted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewDeletedEventArgs)
        Response.Redirect("~/links/links.aspx")
    End Sub
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <div id="body">
        <club:loginbanner id="LoginBanner1" runat="server" />
        <div class="fullwidth">
            <h2>
                Links</h2>
            <p>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ClubSiteDB %>"
                    DeleteCommand="DELETE FROM [Links] WHERE [linkid] = @linkid" InsertCommand="INSERT INTO [Links] ([Name], [URL]) VALUES (@Name, @URL)"
                    SelectCommand="SELECT * FROM [Links] ORDER BY [linkid]" UpdateCommand="UPDATE [Links] SET [Name] = @Name, [URL] = @URL WHERE [linkid] = @linkid">
                    <DeleteParameters>
                    </DeleteParameters>
                    <UpdateParameters>
                        <asp:Parameter Name="Name" Type="String" />
                        <asp:Parameter Name="URL" Type="String" />
                        <asp:Parameter Name="linkid" Type="Int32" />
                    </UpdateParameters>
                    <InsertParameters>
                        <asp:Parameter Name="Name" Type="String" />
                        <asp:Parameter Name="URL" Type="String" />
                    </InsertParameters>
                </asp:SqlDataSource>
               </p>
                <div style="margin-left:30px">
            <asp:DataList ID="DataList1" runat="server" DataKeyField="linkid" DataSourceID="SqlDataSource1">
                <HeaderTemplate>
                
                <ul>
                </HeaderTemplate>
                <ItemTemplate>
                    <li>
                    <asp:HyperLink ID="HyperLink1" Target="_blank" Text='<%# Eval("Name") %>' NavigateUrl='<%# Eval("URL") %>' runat="server"></asp:HyperLink>
                    <% If User.IsInRole("Administrators") Then%>
                        <br />
                        <asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl='<%# "~/links/links.aspx?editid=" & Eval("linkid").ToString() %>'>Edit </asp:HyperLink>
                        <asp:HyperLink ID="HyperLink3" runat="server" NavigateUrl='<%# "~/links/links.aspx?deleteid=" & Eval("linkid").ToString() %>'>Delete </asp:HyperLink>
                        <br />
                    <% End If%>
                    </li>
                </ItemTemplate>
                <FooterTemplate>
                
                </ul>
                </FooterTemplate>
            </asp:DataList></div><br />
        </div>
        <div runat="server" id="EditPanel" class="fullwidth">
            <h2>
                Edit Links</h2>
            <p>
                <asp:FormView ID="FormView1" runat="server" DataKeyNames="linkid" DataSourceID="SqlDataSource2"
                    DefaultMode="Insert" OnItemUpdated="FormView1_ItemUpdated" OnItemInserted="FormView1_ItemInserted" OnItemDeleted="FormView1_ItemDeleted">
                    <EditItemTemplate>
                        <strong>Name</strong>:
                        <asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="NameTextBox"
                            ErrorMessage="Please enter a link name">*</asp:RequiredFieldValidator><br />
                        <strong>URL</strong>:
                        <asp:TextBox ID="URLTextBox" runat="server" Text='<%# Bind("URL") %>'></asp:TextBox>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Please Enter a URL" ControlToValidate="URLTextBox">*</asp:RequiredFieldValidator>
                        <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="URLTextBox"
                            ErrorMessage="Please Enter A Valid URL" ValidationExpression="http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?">*</asp:RegularExpressionValidator><br />
                        <br />
                        <asp:ValidationSummary ID="ValidationSummary2" runat="server" />
                        <br />
                        <br />
                        <club:RolloverButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
                            Text="Update"></club:RolloverButton>
                        <club:RolloverButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
                            Text="Cancel"></club:RolloverButton>
                    </EditItemTemplate>
                    <InsertItemTemplate>
                        <strong>Name</strong>:
                        <asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="NameTextBox"
                            ErrorMessage="Please Enter Your Name">*</asp:RequiredFieldValidator><br />
                        <strong>URL</strong>:
                        <asp:TextBox ID="URLTextBox" runat="server" Text='<%# Bind("URL") %>'></asp:TextBox>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="URLTextBox"
                            ErrorMessage="Please Enter a URL">*</asp:RequiredFieldValidator>
                        <br />
                        <br />
                        <asp:ValidationSummary ID="ValidationSummary1" runat="server" />
                        <br />
                        <br />
                        <club:RolloverButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"
                            Text="Insert"></club:RolloverButton>
                        <club:RolloverButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
                            Text="Cancel"></club:RolloverButton>
                    </InsertItemTemplate>
                </asp:FormView>
                &nbsp;</p>
            <p>
                <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ClubSiteDB %>"
                    DeleteCommand="DELETE FROM [Links] WHERE [linkid] = @linkid" InsertCommand="INSERT INTO [Links] ([Name], [URL]) VALUES (@Name, @URL)"
                    SelectCommand="SELECT * FROM [Links] WHERE ([linkid] = @linkid) ORDER BY [linkid]"
                    UpdateCommand="UPDATE [Links] SET [Name] = @Name, [URL] = @URL WHERE [linkid] = @linkid">
                    <DeleteParameters>
                        <asp:Parameter Name="linkid" Type="Int32" />
                    </DeleteParameters>
                    <UpdateParameters>
                        <asp:Parameter Name="Name" Type="String" />
                        <asp:Parameter Name="URL" Type="String" />
                        <asp:Parameter Name="linkid" Type="Int32" />
                    </UpdateParameters>
                    <SelectParameters>
                        <asp:QueryStringParameter Name="linkid" QueryStringField="editid" Type="Int32" />
                    </SelectParameters>
                    <InsertParameters>
                        <asp:Parameter Name="Name" Type="String" />
                        <asp:Parameter Name="URL" Type="String" />
                    </InsertParameters>
                </asp:SqlDataSource>
                &nbsp;</p>
        </div>
    </div>
</asp:Content>
